package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallFootprint;
import org.linlinjava.litemall.db.mapper.LitemallFootprintDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallFootprintService {
    @Resource
    private LitemallFootprintDao footprintDao;

    public List<LitemallFootprint> queryByAddTime(Integer userId, Integer page, Integer size) {
    	LambdaQuery<LitemallFootprint> query = footprintDao.createLambdaQuery();
    	query.andEq(LitemallFootprint::getUserId, userId);
    	query.orderBy(LitemallFootprint.Column.addTime.desc());
    	query.limit(page, size);
    	return query.select();
		/*
		 * LitemallFootprintExample example = new LitemallFootprintExample();
		 * example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
		 * example.setOrderByClause(LitemallFootprint.Column.addTime.desc());
		 * PageHelper.startPage(page, size); return
		 * footprintMapper.selectByExample(example);
		 */
    }

    public LitemallFootprint findById(Integer id) {
    	return footprintDao.single(id);
        //return footprintMapper.selectByPrimaryKey(id);
    }

    public LitemallFootprint findById(Integer userId, Integer id) {
    	LitemallFootprint entity = new LitemallFootprint();
    	entity.setId(id);
    	entity.setUserId(userId);
    	entity.setDeleted(false);
    	return footprintDao.templateOne(entity);
		/*
		 * LitemallFootprintExample example = new LitemallFootprintExample();
		 * example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(
		 * false); return footprintMapper.selectOneByExample(example);
		 */
    }

    public void deleteById(Integer id) {
    	
    	LitemallFootprint entity = new LitemallFootprint();
    	entity.setId(id);
    	entity.setDeleted(true);
    	footprintDao.updateTemplateById(entity);
    	
        //footprintMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallFootprint footprint) {
        footprint.setAddTime(LocalDateTime.now());
        footprint.setUpdateTime(LocalDateTime.now());
        footprintDao.insertTemplate(footprint,true);
        //footprintMapper.insertSelective(footprint);
    }

    public List<LitemallFootprint> querySelective(String userId, String goodsId, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallFootprint> query = footprintDao.createLambdaQuery();
    	 if (!StringUtils.isEmpty(userId)) {
    		 query.andEq(LitemallFootprint::getUserId, Integer.valueOf(userId));
         }
         if (!StringUtils.isEmpty(goodsId)) {
        	 query.andEq(LitemallFootprint::getGoodsId, Integer.valueOf(goodsId));
         }
    	query.orderBy(LitemallFootprint.Column.addTime.desc());
    	query.limit(page, size);
    	return query.select();
    	
    	
		/*
		 * LitemallFootprintExample example = new LitemallFootprintExample();
		 * LitemallFootprintExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(userId)) {
		 * criteria.andUserIdEqualTo(Integer.valueOf(userId)); } if
		 * (!StringUtils.isEmpty(goodsId)) {
		 * criteria.andGoodsIdEqualTo(Integer.valueOf(goodsId)); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, size); return
		 * footprintMapper.selectByExample(example);
		 */
    }
}
